package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.dao.FamilyDao;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.family.FamilyMember;

public class FamilyServlet extends BaseServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String m = request.getParameter("m");
		if ("list".equals(m)) {
			process(request, response, "/page/family/family_list.jsp");
		} else if ("input".equals(m)) {
			process(request, response, "/page/family/family_add.jsp");
		} else if ("add".equals(m)) {
			add(request, response);
			process(request, response, "/page/family/family_list.jsp");
		} else if ("get".equals(m)) {
			process(request, response, "/page/family/family_edit.jsp");
		} else if ("edit".equals(m)) {
			edit(request, response);
			process(request, response, "/page/family/family_list.jsp");
		} else if ("del".equals(m)) {
			del(request, response);
			process(request, response, "/page/family/family_list.jsp");
		}else if("find".equals(m)){ 
			find(request,response);
		} else {
			error(request, response);
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {
			// 接收页面传递过来的参数
			String areacode = request.getParameter("areacode");
			String familyattribute = request.getParameter("familyattribute");
			String population = request.getParameter("population");
			String farmpopulation = request.getParameter("farmpopulation");
			String familyaddress = request.getParameter("familyaddress");
			String creattime = request.getParameter("creattime");
			String registrar = request.getParameter("registrar");
			String familycode = request.getParameter("first")+request.getParameter("next");
			System.out.println("页面传过来的家庭编号"+familycode);
			String familyholdername = request.getParameter("familyholdername");

			Family family = new Family();
			// 调用添加方法
			family.addFamily(areacode, familyattribute, population,
					farmpopulation, familyaddress, creattime, registrar,
					familycode, familyholdername);
			//添加户主姓名的时候顺便加到农合档案里面-------报错鸭鸭鸭，下次改
			FamilyMember familyMember = new FamilyMember();
			// 调用添加方法
			familyMember.setName(familyholdername);
			familyMember.setFamilycode(familycode);
			familyMember.setMembercode(familycode+"-1");//默认户主的户内编号为第一个
			/**
			 * 由于编辑那边不能为空，所以把下面设值为empty String类型，不知道为啥，数据库设值了没有用
			 */
			familyMember.setCmscode("");
			familyMember.setEducationallevel("");
			familyMember.setFarmtype("");
			familyMember.setHealth("");
			familyMember.setIdcard("");
			familyMember.setMarried("");
			familyMember.setMemberattribute("");
			familyMember.setNation("");
			familyMember.setPhone("");
			familyMember.setRelative("户主");
			familyMember.setSex("");
			familyMember.setWorkunit("");
			System.out.println("瞧瞧有没有设值到家庭成员里面"+familyMember);
			familyMember.addFamilyholder(familyMember);
		} catch (Exception e) {
			error(request, response);
		}
	}

	private void edit(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		try {

			String areacode = request.getParameter("areacode");
			String familyattribute = request.getParameter("familyattribute");
			String population = request.getParameter("population");
			String farmpopulation = request.getParameter("farmpopulation");
			String familyaddress = request.getParameter("familyaddress");
			String creattime = request.getParameter("creattime");
			String registrar = request.getParameter("registrar");
			String familycode = request.getParameter("familycode");
			String familyholdername = request.getParameter("familyholdername");
			Family family = new Family();
			family.edit(areacode, familyattribute, population, farmpopulation, familyaddress, creattime, registrar, familycode, familyholdername);
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	private void del(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			// 接收行政区域编码+行政区域名称，行政区域名称有可能出现中文乱码
			String familycode = req.getParameter("id");
			Family family = new Family();
			family.del(familycode);
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
	
	private void find(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String familyholdername = request.getParameter("familyholdername");
			System.out.println("页面传过来的查询字段是啥"+familyholdername);
			if(familyholdername != ""){
				FamilyDao familyDao = new FamilyDao();
		        List<Family> list = familyDao.findListByFamilyholdername(familyholdername);//怎么查查查，要把前台的值搞过来where啥啥啥查询
		        System.out.println("看查出个什么"+list);
		        request.setAttribute("model", list);
		        process(request, response, "/page/family/family_find_list.jsp");
			}else{
		        process(request, response, "/page/family/family_list.jsp");
			}
			
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
}
